﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblItemsDAO
    {
        private DBService _db = null;
        public TblItemsDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool InsUpdItem(TblItems Item, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItems_InsUpd",
                    new string[] { "@ItemID", "@IsInactive", "@ItemName", "@ItemNumber", "@QuantityOnHand", "@ValueOnHand", "@ItemDescription", "@CateID", "@BaseSellingPrice", "@PriceIsInclusive", "@SellTaxCodeID", "@mode" },
                    new object[] { Item.ItemID, Item.IsInactive, Item.ItemName, Item.ItemNumber, Item.QuantityOnHand, Item.ValueOnHand, Item.ItemDescription, Item.CateID, Item.BaseSellingPrice, Item.PriceIsInclusive, Item.SellTaxCodeID, mode },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return flag;
        }

        public bool InsImageItem(TblItemImage imgItem, int mode)
        {
            bool flag = false;
            try {
                _db.ExeSP_NonQuery("sp_TblItemImage_Ins",
                    new string[] { "@p_ItemID", "@p_ImageURL" },
                    new object[] { imgItem.ItemID, imgItem.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetCategory()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblCategories", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetItems()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select vItems.* , TblCategories.CateName from vItems left join TblCategories on vItems.CateID = TblCategories.CateID where TblCategories.IDGroup = 1 ",
                    false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetItemsAll()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select vItems.* , TblCategories.CateName from vItems left join TblCategories on vItems.CateID = TblCategories.CateID",
                    false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public TblItems GetItemByID(int ItemID)
        {
            TblItems item = new TblItems();
            try {
                DataTable dt = _db.ExeSP_DataTable("select vItems.* from vItems left join TblItemImage on vItems.ItemID=TblItemImage.ItemID where vItems.ItemID = @ItemID",
                    new string[] { "@ItemID"},
                    new object[] {ItemID}
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ItemID = ItemID;
                    //item.IsInactive = dr["IsInactive"].ToString();
                    item.ItemName = dr["ItemName"].ToString();
                    item.ItemNumber = dr["ItemNumber"].ToString();
                    //item.QuantityOnHand = Convert.ToDouble(dr["QuantityOnHand"]);
                    //item.ValueOnHand = Convert.ToDouble(dr["ValueOnHand"]);
                    item.ItemDescription = dr["ItemDescription"].ToString();
                    item.CateID = Convert.ToInt32(dr["CateID"]);
                    item.BaseSellingPrice = Convert.ToDouble(dr["BaseSellingPrice"]);
                    //item.PriceIsInclusive = dr["PriceIsInclusive"].ToString();
                    item.SellTaxCodeID = Convert.ToInt32(dr["SellTaxCodeID"]);
                    item.ImageURL = dr["ImageURL"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }

        public DataTable GetImgItems(int ItemID)
        {
            DataTable dt = null;
            try {
                dt = _db.ExeSP_DataTable("select * from TblItemImage where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] {ItemID},
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public bool DeleteImgItem(TblItemImage imgItes)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Del",
                    new string[] { "@p_ID" },
                    new object[] { imgItes.ID },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public DataTable GetListItems(int ItemID, string p_ItemName,string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_Lst",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort" },
                    new object[] {ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // Thuc pham chuc nang
        public DataTable GetProduct(int ItemID, string p_ItemName, string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort, int Mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_ProductList",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort", "@Mode" },
                    new object[] { ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort, Mode },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        // Tim kiem theo giá
        public DataTable GetItemPrice(float PriceFrom, float PriceTo, int Mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_searchprice",
                    new string[] { "@PriceFrom", "@PriceTo", "@mode"},
                    new object[] { PriceFrom, PriceTo, Mode },
                    CommandType.StoredProcedure);
            }
            catch ( Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetvItemByID(int ItemID)
        {
            DataTable dt = null;
            try {
                dt = _db.ExeSP_DataTable("select vItems.*, Vote, TinhNangNoiBat from vItems, TblItemDetail " +
                                        " where vItems.ItemID = TblItemDetail.ItemID and vItems.ItemID = @ItemID ",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool DeleteItem(int ItemID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete from TblItems where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
